package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

import com.aishua.lib.task.AsResult;
import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.jsonbean.ResUserOuterSysLoginBean;
import com.rjwh.dingdong.client.bean.localbean.SysLoginBabylistBean;
import com.rjwh.dingdong.client.bean.localbean.UserOuterSysLoginBean;
import com.rjwh.dingdong.client.bean.localbean.UserOuterSysLoginConfig;
import com.rjwh.dingdong.client.bean.localbean.UserOuterSysLoginUserProfile;
import com.rjwh.dingdong.client.bean.localbean.UserOuterSysLoginValidity;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.ThirdPartyLoginModel;
import com.rjwh.dingdong.client.mvp.presenter.IThirdPartyLoginPresenter;
import com.rjwh.dingdong.client.mvp.view.ThirdPartyLoginView;
import com.rjwh.dingdong.client.network.ApiRequest;
import com.rjwh.dingdong.client.util.LogUtil;
import com.rjwh.dingdong.client.util.ToastUtil;

import java.util.List;


/**
 * @author Kay
 * @ClassName ThirdPartyLoginPresenter
 * @Description
 * @date 2016-10-12
 * @history 2016-10-12 author: description:
 */

public class ThirdPartyLoginPresenter extends BasePresenter<ThirdPartyLoginView> implements IThirdPartyLoginPresenter {

    private ThirdPartyLoginModel model;

    public ThirdPartyLoginPresenter(Context context, ThirdPartyLoginView mMvpView) {
        super(context, mMvpView);
        model = new ThirdPartyLoginModel(this);
    }

    /**
     * 第三方登录
     *
     * @param uid      id
     * @param nickname 昵称
     * @param avatar   头像
     * @param apptype  平台类型
     */
    @Override
    public void doUserOuterSysLogin(String uid, String nickname, String avatar, String apptype) {
        if (isViewAttached()) {
            getMvpView().showLoading();
            model.doUserOuterSysLogin(uid, nickname, avatar, apptype);
        }
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (isViewAttached()) {
            getMvpView().dismissLoading();
            switch (businessType) {
                case NetConstant.USEROUTERSYSLOGIN:
                    ResUserOuterSysLoginBean resUserOuterSysLoginBean = (ResUserOuterSysLoginBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS &&
                            resUserOuterSysLoginBean != null && resUserOuterSysLoginBean.getData() != null) {
                        UserOuterSysLoginBean data = resUserOuterSysLoginBean.getData();

                        UserOuterSysLoginConfig initconfig = data.getInitconfig();
                        UserOuterSysLoginUserProfile userprofile = data.getUserprofile();
                        UserOuterSysLoginValidity validity = data.getValidity();
                        List<SysLoginBabylistBean> babylist = data.getBabylist();

                        if (initconfig != null) {
                            String isbind = initconfig.getIsbind();
                            if (!AsStrUtils.isEmpty(isbind) && isbind.equals("1")) {
                                //已绑定手机号,存储数据到本地
                                if (userprofile != null) {
                                    MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERID, userprofile.getUserid());
                                    MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERPHONE, userprofile.getPhone());
                                    MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERNAME, userprofile.getNickname());
                                    MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERIMAGE, userprofile.getAvatar());
                                    MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERCOMPLETE, userprofile.getComplete());
                                    MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERLEVEL, userprofile.getUserlevel());
                                }

                                if (validity != null) {
                                    MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USER_TOKEN, validity.getToken());
                                    MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USER_HASPWD, validity.getHaspwd());
                                }

                                if (babylist != null && !babylist.isEmpty()) {

                                    for (SysLoginBabylistBean sysLoginBabylistBean : babylist) {
                                        String chief = sysLoginBabylistBean.getChief();
                                        if (chief.equals("1")) {//取当前绑定的宝宝
                                            MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_BABYID, sysLoginBabylistBean.getBabyid());
                                            MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_BABYNAME, sysLoginBabylistBean.getBabyname());
                                            MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_BABYSEX, sysLoginBabylistBean.getSex());
                                            MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_BABYBIRTHDAY, sysLoginBabylistBean.getBirthday());
                                            MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERCHENGWEI, sysLoginBabylistBean.getTitle());
                                            MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_BABYAVATAR, sysLoginBabylistBean.getBabyavatar());
                                            MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_BABYAGE, sysLoginBabylistBean.getBabyage());
                                        }
                                    }
                                }

                                MyApplication.spUtil.setBoolPreference(getContext(), LocalConstant.SP_IS_LOGINED, true);
                                try {
                                    ApplicationInfo appInfo = getContext().getPackageManager()
                                            .getApplicationInfo(getContext().getPackageName(),
                                                    PackageManager.GET_META_DATA);
                                    String msg = appInfo.metaData.getString("PUSH_APPID");
                                    LogUtil.e("ThirdPartyLoginPresenter", "PUSH_APPID:" + msg);
                                    if (!AsStrUtils.isEmpty(msg)) {
                                        LogUtil.d("ThirdPartyLoginPresenter", "绑定推送");
                                        ApiRequest.pushBindingForAppId(msg, MyApplication.spUtil.getStrPreferenceByParamName(getContext(), LocalConstant.SP_CLIENTID), null);
                                    }
                                } catch (PackageManager.NameNotFoundException e) {
                                    e.printStackTrace();
                                }
                                getMvpView().loadData();
                            } else {
                                //未绑定
                                getMvpView().toBindMobileNumber();
                            }
                        }
                    } else if (!AsStrUtils.isEmpty(mAsResult.getResultMsg())) {
                        ToastUtil.showToast(getContext(), mAsResult.getResultMsg());
                    }
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }
}
